﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B5
{
    abstract class Graph
    {
        public abstract double getArea();
    }
    interface Idiagram
    {
        bool isLegal();
        
    }
    class Triangle : Graph,Idiagram
    {
        public double pin1 { set; get; }
        public double pin2 { set; get; }
        public double pin3 { set; get; }
        public Triangle(double pin1, double pin2, double pin3)
        {
            this.pin1 = pin1;
            this.pin2 = pin2;
            this.pin3 = pin3;
        }
        public bool isLegal()
        {
            if (pin1 == 0 || pin2 == 0 || pin3 == 0) { return false; }
            if (pin1 + pin2 <= pin3) { return false; }
            if (pin3 + pin2 <= pin1) { return false; }
            if (pin1 + pin3 <= pin2) { return false; }
            return true;
        }
        public override double getArea()
        {
            if (isLegal())
            {
                double d = (pin1 + pin2 + pin3) / 2.0;
                return Math.Sqrt(d * (d - pin1) * (d - pin2) * (d - pin3));
            }
            return -1;
        }
    }
    class Rectangle : Graph, Idiagram
    {
        public Rectangle(double len, double wid)
        {
            this.len = len;
            this.wid = wid;
        }
        public double len { set; get; }
        public double wid { set; get; }
        public bool isLegal()
        {
            if (len == 0 || wid == 0) { return false; }
            return true;
        }
        public override double getArea()
        {
            if (isLegal()) { return len * wid; }
            return -1;
        }
    }
    class Square : Graph, Idiagram
    {
        public Square(double side)
        {
            this.side = side;
        }
        public double side { set; get; }
        public bool isLegal()
        {
            if (side == 0) { return false; }
            return true;
        }
        public override double getArea()
        {
            if (isLegal()) { return side * side; }
            return -1;
        }
    }
    class Factory
    {
        public static Graph Getproduct(string kind,double size)
        {
            Graph graph = null;
            switch (kind.ToLower()) {
                case "square":
                    graph = new Square(size);break;
                case "rectangle":
                    graph = new Rectangle(size,size); break;
                case "triangle":
                    graph = new Triangle(size, size,size); break;
                default: break;
            }
            return graph; 
        }
    }
        class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            double flag = 0;
            Graph[] graph = new Graph[10];
            double sum = 0;
            for (int i = 0; i < 10; i++)
            {
                flag = r.Next() % 3;
                switch (flag) {
                    case 0:graph[i] = Factory.Getproduct("Square", r.NextDouble() * 10);break;
                    case 1: graph[i] = Factory.Getproduct("Rectangle", r.NextDouble() * 10); break;
                    case 2: graph[i] = Factory.Getproduct("Triangle", r.NextDouble() * 10); break;
                }
                sum += graph[i].getArea();
                Console.WriteLine(graph[i].getArea().ToString());
            }
            Console.WriteLine(sum.ToString());
            Console.ReadLine();
        }
    }
}
